﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jzy.Model.DocManger;

using Jzy.DAL.DocManger;
using System.Web.UI.WebControls;

using Frame.Lib.Core.Tree;
using Frame.Lib.Core.Right;
using Frame.Lib.Enums;
using Frame.Lib.Web;
using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Jzy.DAL;

namespace Jzy.BLL.DocManger
{
    public class DirBO : TreeBO<DirInfo>
    {
        private DirDAL _dir;
        public DirBO()
            : base("DocDir")
        {
            _dir = new DirDAL();
        }

        #region 带权限控制
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tv"></param>
        /// <param name="userID"></param>
        /// <param name="rightName"></param>
        public void BindTreeView(TreeView tv, List<int> lstBindIDs,bool hasCheckbox)
        {
            tv.CssClass = "treeview";
            List<DirInfo> list = base.GetList();
            IEnumerable<DirInfo> lstPrivate = list.Where(T => T.IsPrivate == true && T.CreatorID == AppSession.UserID);
            List<DirInfo> lstPublic = list.Where(T => T.IsPrivate == false).ToList();

            if (lstPublic.Count == 0)//增加顶级目录,创建者就是登录用户
            {
                return;
            }
            if (AppSession.UserID != Constants.ADMIN_ID)//如非超级管理员，则进行权限判断
            {
                if (lstBindIDs != null)
                {
                    for (int i = 0; i < lstPublic.Count; i++)
                    {
                        int id = lstPublic[i].ID;

                        if (lstBindIDs.IndexOf(id) < 0)
                        {
                            lstPublic.RemoveAt(i);
                            i--;
                        }
                    }
                }
            }
            tv.ShowLines = true;
            tv.Nodes.Clear();

            GetNode(tv, lstPublic, null, 0,hasCheckbox);

            if (tv.Nodes.Count > 0)
            {

                //处理私有文档
                int rootID = (int)base.GetRootID();
                TreeNode mydoc = new TreeNode("我的文档", tv.Nodes[0].Value);
                string strImageUrl = GlobalVar.WebAppPath + "common/images/Common/folder.gif";
                if (hasCheckbox)
                {
                    mydoc.Text = "我的文档";
                    mydoc.SelectAction = TreeNodeSelectAction.None;
                }
                else
                {
                    mydoc.Text = "<span onclick='SelectNode(\"img0\")'> <img id = 'img0'  alt='' style='border-width: 0;' align='absmiddle' src='" + strImageUrl + "' /> 我的文档</span>";
                    mydoc.NavigateUrl = GlobalVar.WebAppPath + "DocManager/Dir/List.aspx?IsPrivate=true";

                }

                tv.Nodes.Add(mydoc);

                GetNode(tv, lstPrivate, mydoc, rootID, hasCheckbox);
            }

        }
        #endregion

        private void GetNode(TreeView tv, IEnumerable<DirInfo> list, TreeNode node, int upperID,bool hasCheckbox)
        {
            tv.NodeStyle.ImageUrl = "";
            string strImageUrl = GlobalVar.WebAppPath + "common/images/Common/folder.gif";

            IEnumerable<DirInfo> lst = list.Where(Info => Info.UpperID == upperID).OrderBy(Info => Info.Orders);
            foreach (DirInfo info in lst)
            {
                TreeNode ndTmp = new TreeNode();
                ndTmp.ToolTip = info.Name;
                ndTmp.Value = info.ID.ToString();
                if (hasCheckbox)
                {
                    ndTmp.Text = info.Name;
                    ndTmp.SelectAction = TreeNodeSelectAction.None;
                }
                else
                {
                    ndTmp.Text = "<span onclick='SelectNode(\"img" + info.ID + "\")'> <img id = 'img" + info.ID + "'  alt='' style='border-width: 0;' align='absmiddle' src='" + strImageUrl + "' /> " + info.Name.ToString() + "</span>";
                    ndTmp.NavigateUrl = GlobalVar.WebAppPath + "DocManager/Document/List.aspx?DirID=" + StrUtil.EncryptUrl(info.ID);
                }
                if (node == null)
                {
                    tv.Nodes.Add(ndTmp);
                }
                else
                {
                    node.ChildNodes.Add(ndTmp);
                }
                GetNode(tv, list, ndTmp, info.ID,hasCheckbox);
            }
        }

        public Dictionary<int, int> GetCreatorIDs(List<int> ids)
        {
            Dictionary<int, int> lstCreatorIDs = _dir.GetCreatorIDs(ids);
            return lstCreatorIDs;
        }

        public IEnumerable<DirInfo> GetList(int upperID, int userID, PagerSortSetting setting)
        {
            IEnumerable<DirInfo> list = base.GetList(upperID);

            if (userID != Constants.ADMIN_ID)
            {
                RecordRightBO right = new RecordRightBO(OTableName.DocDirRight);
                List<int> lstDirIDs = right.GetBindIDs(userID, RecordRightName.Browse, RightOwnerType.User);//能浏览的文档ID集合
                list = list.Where(Info => Info.CreatorID == userID || lstDirIDs.Contains(Info.ID));
            }
            if (setting != null)
            {
                setting.RecordCount = list.Count();
                list = list.Skip(setting.SkipCount).Take(setting.PageSize);
            }
            return list;
        }

        public bool Exist(int upperID, string strName, string id, bool isPrivate)
        {
            return _dir.Exist(upperID, strName, id, isPrivate);
        }

        public bool ExistCode(string code, string id, bool isPrivate)
        {
            return _dir.ExistCode(code, id, isPrivate);
        }
    }
}
